package com.wcity.manage.web.module;

import com.wcity.manage.common.utils.DwzUtil;
import com.wcity.manage.common.utils.RespUtil;
import com.wcity.manage.common.utils.StringUtil;
import com.wcity.manage.dao.dal.model.PageModel;
import com.wcity.manage.dao.mapper.ModuleManageMapper;
import com.wcity.manage.dao.mapper.ModuleMapper;
import com.wcity.manage.dao.mapper.UserMapper;
import com.wcity.manage.dao.mapper.UserModuleMapper;
import com.wcity.manage.dao.model.Module;
import com.wcity.manage.dao.model.ModuleManage;
import com.wcity.manage.dao.model.User;
import com.wcity.manage.service.ModuleService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by jn on 2016/12/9.
 * 用户模块管理
 */
@Controller
public class ModuleManageAct {

    /**
     * 用户模块管理新增
     * @param manage
     * @param response
     */
    @RequestMapping("module_manage_add")
    public void moduleManageAdd(ModuleManage manage, HttpServletResponse response,String name){

        String errorMsg = moduleService.addModuleManage( manage, name );

        if ( StringUtil.isBlank( errorMsg ) ) {
            RespUtil.sendJson( response, DwzUtil.getRetAjaxJson( "200", "添加成功", "module_manage_list", "", "closeCurrent", "" ) );
        } else {
            RespUtil.sendJson( response, DwzUtil.getRetAjaxJson( "300", errorMsg, "", "", "", "" ) );
        }
    }

    /**
     * 用户模块管理新增跳转
     * @return
     */
    @RequestMapping("to_module_manage_add")
    public ModelAndView toModuleManageAdd(){
        List<User> list = userMapper.findAll();
        List<Module> modules = moduleMapper.findChildren( 0 );
        ModelAndView mav = new ModelAndView("module/modulemanageadd");
        mav.addObject("users",list);
        mav.addObject("modules",modules);
        mav.addObject("moduleTree",DwzUtil.getModuleTree( modules, new StringBuffer()));
        return mav;
    }

    /**
     * 用户模块管理修改
     * @param manage
     * @param response
     */
    @RequestMapping("module_manage_edit")
    public void moduleManageEdit(ModuleManage manage, String name, HttpServletResponse response){
        String errorMsg = moduleService.editModuleManage( manage, name );
        if ( StringUtil.isBlank( errorMsg ) ) {
            RespUtil.sendJson( response, DwzUtil.getRetAjaxJson( "200", "修改成功", "module_manage_list", "", "closeCurrent", "" ) );
        } else {
            RespUtil.sendJson( response, DwzUtil.getRetAjaxJson( "300", errorMsg, "", "", "", "" ) );
        }
    }

    /**
     * 用户模块管理修改跳转
     * @param id
     * @return
     */
    @RequestMapping("to_module_manage_edit")
    public ModelAndView toModuleManageEdit(String id){
        ModuleManage manage = manageMapper.find( Integer.parseInt( id ) );
        List<Module> modules = moduleMapper.findChildren( 0 );
        List<Integer> moduleIds = userModuleMapper.findUserModuleIds( manage.getUserId() );
        ModelAndView mav = new ModelAndView("module/modulemanageedit");
        mav.addObject("manage",manage);
        mav.addObject("moduleTree",DwzUtil.getModuleTree( modules, new StringBuffer(), moduleIds ));
        return mav;
    }

    /**
     * 用户模块管理列表
     * @param manage
     * @param pageModel
     * @return
     */
    @RequestMapping("module_manage_list")
    public ModelAndView moduleManageList(ModuleManage manage,PageModel<ModuleManage> pageModel){
        pageModel.setParams( manage );
        List<ModuleManage> list = manageMapper.findListPage( pageModel );
        ModelAndView mav = new ModelAndView("module/modulemanagelist");
        mav.addObject("list",list);
        mav.addObject("pageModel",pageModel);
        return mav;
    }

    @Resource private ModuleManageMapper manageMapper;
    @Resource private UserMapper userMapper;
    @Resource private ModuleService moduleService;
    @Resource private ModuleMapper moduleMapper;
    @Resource private UserModuleMapper userModuleMapper;

}
